﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using YArchitech.Electric;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class CableLayRuleForm : System.Windows.Forms.Form
	{
		public CableLayRuleForm()
		{
			this.InitializeComponent();
		}

		private void CableLayRuleForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.buttonTip.FlatStyle = FlatStyle.Flat;
			this.buttonTip.Image = YArchitech.Electric.Resources.GetImage("Tip16");
			this.ReadFormDataFromXml();
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			List<XmlElement> elements = xmlHelper.GetElements("CableLayRule");
			this.checkBoxVolumeRatio.Checked = Convert.ToBoolean(xmlHelper.GetAttributeEx("IsChecked", "true"));
			foreach (XmlElement xmlElement in elements)
			{
				string attribute = xmlElement.GetAttribute("Name");
				string attribute2 = xmlElement.GetAttribute("Value");
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				int index = this.dataGridView.Rows.Add(dataGridViewRow);
				dataGridViewRow = this.dataGridView.Rows[index];
				dataGridViewRow.Cells[0].Value = attribute;
				dataGridViewRow.Cells[1].Value = attribute2;
			}
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("CableLayRule");
			xmlHelper.SetAttribute("IsChecked", this.checkBoxVolumeRatio.Checked.ToString());
			List<XmlElement> elements = xmlHelper.GetElements();
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				string name = dataGridViewRow.Cells[0].Value.ToString();
				string value = dataGridViewRow.Cells[1].Value.ToString();
				XmlElement xmlElement = elements.Find((XmlElement item) => name == item.GetAttribute("Name"));
				if (xmlElement != null)
				{
					YArchitech.MEP.Electric.Common.XmlHelper.SetAttribute(xmlElement, "Value", value);
				}
			}
			xmlHelper.SaveXml("");
		}

		private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count || e.ColumnIndex < 1 || e.ColumnIndex >= this.dataGridView.Columns.Count)
			{
				e.Cancel = true;
				return;
			}
			this.m_value = Convert.ToString(this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
			this.m_value = (string.IsNullOrWhiteSpace(this.m_value) ? "0" : this.m_value.Trim());
		}

		private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count || e.ColumnIndex < 1 || e.ColumnIndex >= this.dataGridView.Columns.Count)
			{
				return;
			}
			if (this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
			{
				this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.m_value;
				return;
			}
			string text = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim();
			if (string.IsNullOrWhiteSpace(text))
			{
				this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.m_value;
				return;
			}
			if (text == this.m_value)
			{
				return;
			}
			if (Convert.ToDouble(text) > 1.0)
			{
				MessageBox.Show("电缆桥架容积率应介于0~1之间。", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.m_value;
			}
		}

		private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			e.Control.KeyPress -= this.dataGridView_TextBox_KeyPress;
			e.Control.KeyPress += this.dataGridView_TextBox_KeyPress;
		}

		private void dataGridView_TextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (StringTools.IsNumber(e.KeyChar))
			{
				return;
			}
			e.Handled = true;
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			this.SaveFormDataToXml();
			this.buttonTip_Leave(null, null);
		}

		private void buttonTip_Click(object sender, EventArgs e)
		{
			string text = "选中“桥架容积率”，表示敷设时考虑桥架容积率，按最优路径敷设；\n反之，表示敷设时不考虑桥架容积率，按最短路径敷设。";
			this.buttonTip_Leave(null, null);
			this.m_toolTip = new ToolTip();
			this.m_toolTip.IsBalloon = true;
			this.m_toolTip.Show(text, this.buttonTip, 0, -70);
		}

		private void buttonTip_Leave(object sender, EventArgs e)
		{
			if (this.m_toolTip != null)
			{
				this.m_toolTip.Hide(this.buttonTip);
				this.m_toolTip.Dispose();
				this.m_toolTip = null;
			}
		}

		private const string XmlNode_Root = "CableLayRule";

		private const string Rule_IsChecked = "IsChecked";

		public ToolTip m_toolTip;

		private string m_value = string.Empty;
	}
}
